class Solution
{
public:
    int countTriplets(vector<int> &nums)
    {
        vector<int> maskCount(1 << 16, 0);
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                ++maskCount[nums[i] & nums[j]];
            }
        }
        int count = 0;
        for (int mask = 0; mask < (1 << 16); ++mask)
        {
            for (int num : nums)
            {
                if ((num & mask) == 0)
                {
                    count += maskCount[mask];
                }
            }
        }
        return count;
    }
};